package com.study.sbsummary.service;

import com.study.sbsummary.wrapper.dingding.DingWrapper;
import com.study.sbsummary.wrapper.dingding.domain.AlarmRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class DingService {

    @Autowired
    private DingWrapper dingWrapper;

    @Value("${spring.profiles.active}")
    String profiles;

    public String sendDing(String content, String[] mobiles) {
        if (!(StringUtils.equals(profiles, "prod") || StringUtils.equals(profiles, "pref"))) {
            return "";
        }
        AlarmRequest alarmRequest = new AlarmRequest();
        alarmRequest.setMsgtype("text");
        AlarmRequest.Text text = new AlarmRequest.Text();
        String currentDateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        text.setContent(profiles + ":" + currentDateStr + " plog异常:" + content);
        alarmRequest.setText(text);
        if (mobiles != null && mobiles.length > 0) {
            AlarmRequest.At at = new AlarmRequest.At();
            at.setAtMobiles(mobiles);
            alarmRequest.setAt(at);
        } else {
            AlarmRequest.At at = new AlarmRequest.At();
            at.setIsAtAll(true);
            alarmRequest.setAt(at);
        }
        return dingWrapper.sendDingDing(alarmRequest);
    }
}
